<?php
/**
 * Goods.php
 *
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2015-2025 山西牛酷信息科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: http://www.niushop.com.cn
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * @author : niuteam
 * @date : 2015.1.17
 * @version : v1.0.0.0
 */

namespace data\service;


use data\service\BaseService as BaseService;
use data\model\NsPromotionTuangouApplyModel;
use data\model\NsPromotionTuangouModel;
use data\model\UserModel;
use data\service\Goods;

class TuangouApply extends BaseService
{

    public $apply;
    public $promotionTuangou;

    public function __construct()
    {
        $this->apply = new NsPromotionTuangouApplyModel();
        $this->promotionTuangou = new NsPromotionTuangouModel();
    }

    /**
     * 活动报名
     * @param $tuangou_id
     * @param $uid
     * @param $shop_id
     * @param $goods_id
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function activityRregistration($tuangou_id, $uid, $shop_id, $goods_id)
    {
        $tuangou = new NsPromotionTuangouModel();
        $data_tuangou = $tuangou->where(['goods_id' => $goods_id, 'is_open' => 1, 'tuangou_id' => $tuangou_id])->find();

        $issetUserApply = $this->apply->where(['uid' => $uid, 'tuangou_id' => $tuangou_id, 'shop_id' => $shop_id])->find();

        if (empty($data_tuangou) || !empty($issetUserApply)) {
            return '活动不存或在已报名过';
        }

        $array = [
            'shop_id' => $shop_id,
            'tuangou_id' => $tuangou_id,
            'uid' => $uid,
            'goods_id' => $goods_id,
            'create_time' => time(),
        ];

        $boole = $this->apply->insert($array);
        if (!$boole) return '创建失败';
        return true;
    }

    /**
     * 报名列表
     * @param int $page_index
     * @param int $page_size
     * @param string $condition
     * @param string $order
     * @return array
     */
    public function Userlist($page_index, $page_size, $condition)
    {

        $tuangou = new NsPromotionTuangouApplyModel();
        $list = $tuangou->pageQuery($page_index, $page_size, $condition, "tuangou_apply_id", '*');

        return $list;
    }

    /**
     * 审计
     * @param $tuangou_apply_id
     * @param $is_open
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function audit($tuangou_apply_id,$is_open)
    {
        return $this->apply->where(["tuangou_apply_id" => $tuangou_apply_id])->update(['is_open' => $is_open]);
    }

    /**
     * 参加拼团人数查询
     * @param $tuangou_id
     */
    public function participantsNumber($tuangou_id)
    {
        $user = new  UserModel();

        $where = [
            "tuangou_id"=>$tuangou_id,
            "is_open"=>1,
        ];
        $data['count']= $this->apply->where($where)->count();

        $array = $this->apply->where($where)->column('uid');
        foreach ($array as $k=>$v){
            $data['data'][$v] = $user->where('uid',$v)->value('user_headimg');
        }
        return $data;
    }

    /**
     * 拼团集合
     */
    public function tuangouGather($shop_id)
    {
        $goods = new Goods();
        $data = $goodsInfo = [];
        $where['shop_id'] = ['eq',$shop_id];
        $where['is_open'] = ['eq',1];
        $where['goods_type_crowd'] = ['in',[1,2]];
        $num = 4;

        for ($i=1;$i<=4;$i++){
            $where['tuangou_type'] = ['eq',$i];
            if($i==4)$num = 9;
            $goods_id_array=$this->promotionTuangou->where($where)->page(1,$num)->order('tuangou_id','desc')->column('goods_id');
            $data['data'][$i]['name']=pintuanNameList($i);
            $data['data'][$i]['Info'] =[];

            foreach ($goods_id_array as $k=>$v){
                $data['data'][$i]['Info'][$k] = $goods->miniatureGoodsInfo($v);
            }
        }
         $array['data'] = array_values($data['data']);
        return $array;
    }
}